package com.ten.mall.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ten.mall.mapper.ItemMapper;
import com.ten.mall.service.ItemService;
import com.ten.mall.vo.GoodsYaodian;
import com.ten.mall.vo.Item;
import com.ten.mall.vo.ItemExample;
import com.ten.mall.vo.ItemExample.Criteria;
import com.ten.mall.vo.KucunResult;
import com.ten.mall.vo.Pihao;
@Service
@Transactional
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemMapper itemMapper;
	@Override
	@Transactional(readOnly=true)
	public Map<String,Object> selectByPages(Integer start, Integer size, String name) {
		// 开始分页
		PageHelper.startPage(start, size);
		ItemExample example =new ItemExample();
		Criteria c =example.createCriteria();
		c.andGoodsStorgeGreaterThanOrEqualTo(1);
		if(StringUtils.isNotBlank(name)){
			c.andGoodsNameLike("%"+name+"%");
		}
		List<Item> list =itemMapper.selectByExample(example);
		PageInfo<Item> info =new PageInfo<>(list);
		Map<String, Object> map =new HashMap<>();
		map.put("list", list);
		map.put("total", info.getTotal());
		return map;
	}

	@Override
	@Transactional(readOnly=true)
	public Item selectById(Integer id) {
		Item i =itemMapper.selectByPrimaryKey(id);
		return i;
	}

	@Override
	public int insertItem(Item record) {
		// TODO Auto-generated method stub
		int i =itemMapper.insert(record);
		return i;
	}

	@Override
	public int updateItem(Item record) {
		// TODO Auto-generated method stub
		int i =itemMapper.updateByPrimaryKeySelective(record);
		return i;
	}

	@Override
	public int deleteItem(Integer id) {
		// TODO Auto-generated method stub
		int i =itemMapper.deleteByPrimaryKey(id);
		return i;
	}
	/**
	 * 药店库存管理
	 */
	@Override
	@Transactional(readOnly=true)
	public List<KucunResult> selectYaodianKucun(Integer start, Integer size, KucunResult res) {
		PageHelper.startPage(start, size);
		List<KucunResult> list =itemMapper.selectYaodianKucun(res);
		return list;
	}
	/**
	 * 通过联合主键查询
	 */
	@Override
	@Transactional(readOnly=true)
	public KucunResult selectYaodianKucun(KucunResult res) {
		List<KucunResult> list =itemMapper.selectYaodianKucun(res);
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public int insertGoodsYaodian(GoodsYaodian record) {
		int i =itemMapper.insertGoodsYaodian(record);
		return i;
	}

	@Override
	@Transactional(readOnly=true)
	public List<Item> selectAll() {
		ItemExample example =new ItemExample();
		List<Item> list =itemMapper.selectByExample(example);
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public List<Item> selectByItemVo(Integer start, Integer size, Integer state) {
		PageHelper.startPage(start, size);
		ItemExample example =new ItemExample();
		Criteria c =example.createCriteria();
		c.andGoodsStorgeGreaterThanOrEqualTo(1);
		c.andGoodsSellEqualTo(state);
		List<Item> list =itemMapper.selectByExample(example);
		return list;
	}

}
